<?php
/**
 * Created by PhpStorm.
 * User: dev
 * Date: 7/10/14
 * Time: 1:07 PM
 */

class Banner_Widget_Banner_Controller extends Base_Controller {

    public $restful = true;

    public function __construct()
    {
        parent::__construct();
        //--Your Code --//
    }


    public function get_index($page = 0, $no = 1){

    	$data['mode'] = "INDEX";
    	$data['banner'] = Banners::take($no)
            ->where('from_date', '<=', new DateTime('today'))
            ->where('to_date', '>=', new DateTime('today'))
            ->where('page', '=', $page)
            ->or_where('page', '=', 0)
            ->raw_where('impressions < max_impressions')
            ->raw_where('clicks < max_clicks')
            ->get();

        return View::make('Banner::widget.banner',$data);

    }
    
    public function get_tabindex($page = 0, $no = 1){
    	$data['mode'] = "TABINDEX"; 
    	$data['banner'] = Banners::take($no)
    	->where('from_date', '<=', new DateTime('today'))
    	->where('to_date', '>=', new DateTime('today'))
    	->where('page', '=', $page)
    	->or_where('page', '=', 0)
    	->raw_where('impressions < max_impressions')
    	->raw_where('clicks < max_clicks')
    	->order_by(DB::raw('RAND()'))
    	->get();
    	
    	return View::make('Banner::widget.banner',$data);
    }

    public function get_clicked($id){
        $id = (int)$id;
        $banner = Banners::find($id);

        if($banner){

            $sql = "update banners set clicks = clicks +1 where id = $id";
            DB::query($sql);

            //--Pass data to Java script --//
            if($banner->link != "#" && $banner->link != ""){
                return $banner->link;
            }
        }



    }
}